<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <PageTools>
          <template #after>
            <el-button type="primary" @click="addbtn">添加角色</el-button>
          </template>
        </PageTools>

        <el-table :data="tableList" border>
          <el-table-column label="用户名" prop="username" />
          <el-table-column label="邮箱" prop="email" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" @click="permbtn(row.id)">设置就角色</el-button>
              <el-button @click="editbtn(row.id)">编辑</el-button>
              <el-button type="danger" @click="delbtn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            :page-size="5"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <addUsers ref="addUsers" :is-show.sync="showDialog" @updateEvnt="getUsersList" />
      <rolesDialog ref="rolesDialog" :show-dialog.sync="roleshow" />
    </div>
  </div>
</template>

<script>
import { usersList, getUsers, delusers } from '@/api/users'
import addUsers from './components/addUsers.vue'
import rolesDialog from './components/rolesDialog.vue'
export default {
  components: {
    addUsers,
    rolesDialog
  },
  data() {
    return {
      tableList: [], // 员工列表数组
      params: {
        _limit: 5,
        _start: 0
      },
      total: 1,
      showDialog: false,
      roleshow: false
    }
  },
  created() {
    this.getUsersList()
  },
  methods: {
    // 分页器改变
    currentChange(page) {
      this.params._start = (page - 1) * 5
      this.getUsersList()
    },
    // 获取员工列表
    async getUsersList() {
      const res = await usersList(this.params)
      //   console.log(43, res)
      this.tableList = res.data
      const data = await getUsers()
      //   console.log('数量', data)
      this.total = data.data
    },
    // 点击添加员工
    addbtn() {
      this.showDialog = true
    },
    //   点击设置角色
    permbtn(id) {
      this.roleshow = true
      this.$refs.rolesDialog.getroles(id)
    },
    // 点击编辑
    editbtn(id) {
      this.showDialog = true
      this.$refs.addUsers.gerUsersId(id)
    },
    // 点击删除
    delbtn(id) {
      this.$confirm('您确定要删除这个员工吗？', '提示', {
        type: 'warning'
      }).then(async() => {
        if (this.tableList.length === 1) {
          this.params._start -= 5
        }
        await delusers(id)
        this.getUsersList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    }
  }
}
</script>

<style>

</style>
